package com.qm.console.component.sensitive.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qm.console.component.exception.BusinessException;
import com.qm.console.component.sensitive.data.SensitiveConfigVO;
import com.qm.console.component.sensitive.data.SensitiveUpdateVO;
import com.qm.console.component.sensitive.service.SensitiveWordConfigService;
import com.qm.console.component.util.HttpInvokeUtil;
import com.qm.console.controller.dto.BaseRsp;
import com.qm.console.controller.dto.ResultEnum;
import com.qm.console.pubdef.constant.UrlConstant;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SensitiveWordConfigServiceImpl implements SensitiveWordConfigService {


    private final String SENSITIVE_URL = "/sensitive";


    @Override
    public Map<String, Object> listSensitiveWordConfigVO(Integer offset, Integer limit, Integer state, String appId,
                                                         String category, String word) {
        appId = appId==null ? UrlConstant.SensitiveAppID : appId;
        String url = UrlConstant.SensitiveServerHost + SENSITIVE_URL + "/list";
        Map<String, String> paramMap = new HashMap<>();

        if (offset != null) {
            paramMap.put("offset", offset.toString());
        }
        if (limit != null) {
            paramMap.put("limit", limit.toString());
        }
        if (state != null) {
            paramMap.put("state", state.toString());
        }
        if (appId != null) {
            paramMap.put("appId", appId);
        }
        if (word != null) {
            paramMap.put("word", word);
        }
        if (category != null) {
            paramMap.put("category", category);
        }

        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != 0
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void saveSensitiveWordConfig(SensitiveConfigVO sensitiveConfigVO) {
        String url = UrlConstant.SensitiveServerHost + SENSITIVE_URL + "/add";
        String req = JSONObject.toJSONString(sensitiveConfigVO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        JSONObject rsp = JSONObject.parseObject(body);
        if (rsp == null || rsp.getInteger("code") != 0) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void rmSensitiveWordConfig(Long sensitiveId, String appId) {
        String url = UrlConstant.SensitiveServerHost + SENSITIVE_URL + "/del";
        Map<String, String> paramMap = new HashMap<>();

        if (sensitiveId != null) {
            paramMap.put("id", sensitiveId.toString());
            paramMap.put("appId", appId);
        }
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != 0) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void updateSensitiveWordConfig(SensitiveUpdateVO sensitiveUpdateVO) {
        String url = UrlConstant.SensitiveServerHost + SENSITIVE_URL + "/update";
        String req = JSONObject.toJSONString(sensitiveUpdateVO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != 0) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

}
